<template>
  <view>
    <category :categoryList="categoryList" @loadMore="loadMore" img-src="img" label="title" :p-status="status"
              :subCategoryList="subCategoryList" @categoryMainClick="categoryMainClick"
              @categorySubClick="categorySubClick"></category>
  </view>
</template>

<script>
import category from "../../../components/qiyue-category/qiyue-category.vue"
import {categoryService} from "../../../api/buyer/Category";
import {mainService} from "../../../api/buyer/main";
import {attachmentService} from "../../../api/buyer/public/attachmentService";
import {BASE_IMG_URL} from "../../../api/service";

export default {
  components: {
    category
  },
  data() {
    return {
      categoryList: [],
      subCategoryList: [],
      status: "more",
      queryParams: {
        currentPage: 1,
        pageSize: 20,
        filters: {
          category: null
        },
        orderBys: [
          {
            property: "createDatetime",
            order: "desc"
          }
        ]
      },
      categoryName: "",
    }
  },
  created() {
    this.init();
  },
  onPullDownRefresh() {
    this.init();
    setTimeout(function () {
      uni.stopPullDownRefresh();
      this.update = false
      this.update = true
    }, 1000);
  },
  methods: {
    init() {

      // console.log(dictList)
      // if(uni.getStorageSync('userInfo')){
      //   let dictList=uni.getStorageSync('userInfo').dicts["商品类别"].children
      //   let  cList=[];
      //   dictList.forEach(dict =>{
      //     cList.push({
      //       "name":dict.dictName,
      //       "subCategoryList":[],
      //     })
      //   })
      //   this.categoryList=cList;
      //   this.categoryName=this.categoryName?this.categoryName:this.categoryList[0].name
      //   this.findCommodityByCateGory(this.categoryName,"load")
      // }else{
      categoryService.findCategory().then(res => {
        let cList = [];
        console.log(res.data)
        res.data.data.forEach(item => {
          if (item.dictName == '商品类别') {
            item.children.forEach(dict => {
              cList.push({
                "name": dict.dictName,
                "subCategoryList": [],
              })
            })
          }
        })
        this.categoryList = cList;
        this.categoryName = this.categoryName ? this.categoryName : this.categoryList[0].name
        this.findCommodityByCateGory(this.categoryName, "load")
      })
      // }


    },
    categoryMainClick(category) {
      console.log(category)
      this.categoryName = category.name
      this.findCommodityByCateGory(this.categoryName, "load")
    },
    loadMore(params) {
      console.log(params)
      this.status = "no-more"
    },
    findCommodityByCateGory(category, type) {
      this.queryParams.filters.category =  category ;
      if (type == "load") {
        this.subCategoryList = []
        this.queryParams.currentPage = 1;
      }
      mainService.findCommoditys(this.queryParams).then((res) => {
        console.log(this.queryParams, category)
        let tempList = []
        let start = this.subCategoryList.length;
        let resData = res.data.datas;
        if (resData && resData.length != 0) {
          resData.forEach((res => {
            // console.log(res,BASE_URL,res.attachments.length!=0?res.attachments[0].attachmentAddr:"")
            let good = {};
            good.id = res.eid
            good.name = "Commodity"
            good.img = ""
            good.title = res.description
            good.littleTitle = res.shopName
            good.amount = "￥" + res.minPrice
            good.amountRemark = (res.saleNum ? res.saleNum : 0) + " 已购买"
            // good.lable = "包邮"
            good.commodity = res
            tempList.push(good)
          }))
          this.subCategoryList = this.subCategoryList.concat(tempList);
          let end = this.subCategoryList.length;
          this.loadImage(start, end)
          this.status = "more"
        } else {
          this.status = "no-more"
        }
      })
    },
    loadImage(s, e) {
      //懒加载图片
      for (let i = s; i < e; i++) {
        if (!this.subCategoryList[i].img || this.subCategoryList[i].img === "") {
          if (this.subCategoryList[i].id && this.subCategoryList[i].name) {
            attachmentService.findFile(this.subCategoryList[i].id, this.subCategoryList[i].name).then((res) => {
              this.subCategoryList[i].img = BASE_IMG_URL + res.data[0].attachmentAddr
            })
          }
        }
      }
    },
    categorySubClick(category) {
      console.log(category);
      uni.navigateTo({
        url: '/pages/buyer/main/goodDetailed?good=' + encodeURIComponent(JSON.stringify(category.commodity)),
        // url: '/pages/staff/application/production/taskList/task-detail'
      })
    }
  },
  mounted() {
    // for(var i=0;i<20;i++){
    //   var subList = [];
    //   for(var j=0;j<30;j++){
    //     subList.push({"name":"分类"+i+":商品"+j,"logo":"https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.soutu123.cn%2Felement_origin_min_pic%2F01%2F36%2F72%2F37573c258815baa.jpg%21%2Ffw%2F700%2Fquality%2F90%2Funsharp%2Ftrue%2Fcompress%2Ftrue&refer=http%3A%2F%2Fpic.soutu123.cn&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1657850731&t=af41153468507d3f07745a0b5825d14e"})
    //   }
    //   this.categoryList.push({"name":"分类"+i,"subCategoryList":subList})
    // }
    // this.subCategoryList = this.categoryList[0].subCategoryList;
  }
}
</script>

<style scoped>

</style>